
package CtrlLayer;
import ModelLayer.*;
import DBLayer.*;
import java.util.ArrayList;

/**
 * @version 12/22/2011
 * @author Gruppe 4
 */

public class CtrlProduct {
    
    private IFDBProduct dbProduct;
    private CtrlSupplier ctrlSupplier;
    
    public CtrlProduct()
    {
        dbProduct = new DBProduct();
        ctrlSupplier = new CtrlSupplier();
    }
    
//    public ArrayList<Product> findProducts(int productId, String wClause, boolean retrieveAssociation, String productType)
//    {
//        return dbProduct.findProducts(productId, wClause, retrieveAssociation, productType);
//    }
//    
    
    public ArrayList<ProductDescription> findProductDescriptionByPType(String proType, boolean retrieveAssociation)
    {
        return dbProduct.findProductDescriptionPType(proType, retrieveAssociation);
    }
    
    /**
     * NOT YET IMPLEMENTED!!
     * Method call to return all favorite product
     * @return returns an ArrayList with favorite products
     */
    public ArrayList<ProductDescription> getFavoriteProducts()
    {
        return null;
    }        
            
    public ProductDescription findProduct(int productId, boolean  retrieveAssociation)
    {
        return dbProduct.findProductDescription(productId, retrieveAssociation);
    }
            
    public void insertNewProductDescription(String name, double purchasePrice, double salesPrice, 
              String description, boolean favorite, int supplierId, String productType)
      {    
          
           ProductDescription proDescObj = new ProductDescription();
           
           proDescObj.setName(name);
           proDescObj.setPurchasePrice(purchasePrice);
           proDescObj.setSalesPrice(salesPrice);
           proDescObj.setDescription(name);
           proDescObj.setFavorite(favorite);
           proDescObj.setSupplier(ctrlSupplier.findSupplier(supplierId, false));
           proDescObj.setProductType(productType);
           
           Pipe product = new Pipe();
           product.setDiameter(10);
           product.setLength(100);
           
           DBConnection con = DBConnection.getInstance();
           try
           {
                DBConnection.startTransaction();
                dbProduct.insertProduct(proDescObj, product);
                DBConnection.commitTransaction();
           }
           catch(Exception e)
           {
               System.out.println(e.getMessage());
               DBConnection.rollbackTransaction();
                
           }
      }
}
